﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using BoardController.Models;
using ReactiveUI.Fody.Helpers;
using RosSharp.RosBridgeClient;
using RosSharp.RosBridgeClient.MessageTypes.Geometry;

namespace BoardController.ViewModels
{
  public  class RosAllWindowVM:ViewModelBase
    {
        public RosAllWindowVM()
        {
            try
            {
                Type baseWindowType = typeof(Message);
                Assembly assembly = Assembly.GetAssembly(baseWindowType);

                Type[] types = assembly.GetTypes();

                for (int i = 0; i < types.Length; i++)
                {
                    Type type = types[i];
                    if (!baseWindowType.IsAssignableFrom(type))
                    {
                        continue;
                    }
                    RosTypeModel rosTypeModel = new RosTypeModel();
                    rosTypeModel.MessageType = type;
                    FieldInfo field = type.GetField("RosMessageName");
                    if (field == null)
                    {
                        continue;
                    }
                    object objRosName = field.GetRawConstantValue();
                    if (objRosName == null)
                    {
                        continue;
                    }

                    rosTypeModel.TypeName = (string)objRosName;
                    this.ObservableRosTypeModels.Add(rosTypeModel);
                    if (this.ObservableRosTypeModels.Count != 0)
                    {
                        SelectRosTypeModel = this.ObservableRosTypeModels[0];
                    }
                }
            }
            catch (Exception exception)
            {
                SubError += exception.StackTrace + "\r\n";
            }


        }

        /// <summary>
        /// RosPc的IP
        /// </summary>
        [Reactive] public string IP { get; set; } = "localhost";

        [Reactive] public int Port { get; set; } = 9090;

        [Reactive] public string Topic { get; set; } = "/turtle1/cmd_vel";

        [Reactive] public string TopicID { get; set; } = "";

        [Reactive] public string TopicPoint { get; set; } = "";

        /// <summary>
        ///筛选
        /// </summary>
        [Reactive] public string Screen { get; set; }


        [Reactive] public string SubContent { get; set; } = "";

        [Reactive] public string SubError { get; set; } = "";

        /// <summary>
        /// 存放Ros消息类型
        /// </summary>
        [Reactive] public ObservableCollection<RosTypeModel> ObservableRosTypeModels { get; set; } = new ObservableCollection<RosTypeModel>();

        /// <summary>
        /// 选中的消息
        /// </summary>
        [Reactive] public RosTypeModel SelectRosTypeModel { get; set; } = new RosTypeModel();
    }
}
